package com.hwj.service.Impl;

import com.hwj.entity.Likes;
import com.hwj.entity.Post;
import com.hwj.mapper.LikesMapper;
import com.hwj.mapper.PostMapper;
import com.hwj.service.LikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikesServiceImpl implements LikesService {
    @Autowired
    LikesMapper likesMapper;
    @Autowired
    PostMapper postMapper;

    @Override
    public boolean set(Likes likes) {
        Likes likes1 = likesMapper.selectUserLike(likes);
        Post post = postMapper.selectByPrimaryKey(likes.getFid());
        Long count = post.getLikes();
        if(likes1==null){
            likesMapper.insert(likes);
            count++;
        }else {
            likesMapper.deleteByPrimaryKey(likes1.getId());
            count--;
        }
        post.setLikes(count);
        postMapper.updateByPrimaryKeySelective(post);
        if(likes1!=null)
            return false;
        return true;
    }

    @Override
    public boolean flag(Likes likes) {
        Likes likes1 = likesMapper.selectUserLike(likes);
        if(likes1!=null)
            return true;
        return false;
    }


}
